package com.thinkTank.oa.lucene;


import java.util.List;


@SuppressWarnings("serial")
public class Pagination implements java.io.Serializable {
	
	public static final int DEF_COUNT = 20;
	public static final int DEF_TOOLCOUNT = 8;  //每一页显示工具条个数
	
	private int totalCount = 0; //记录数
	private int pageSize = 20;  // 每页数量
	private int pageNo = 1;	  //当前页
	private int toolNo = 1; //当前工具条id
	private int pageNos = 1; //分页总数
	
	/**
	 * 当前页的数据
	 */
	@SuppressWarnings("unchecked")
	private List list;
	
	
	public Pagination() {
	}

	public Pagination(int pageNo, int pageSize, int totalCount) {
		if (totalCount <= 0) {
			this.totalCount = 0;
		} else {
			this.totalCount = totalCount;
		}
		if (pageSize <= 0) {
			this.pageSize = DEF_COUNT;
		} else {
			this.pageSize = pageSize;
		}
		if (pageNo <= 0) {
			this.pageNo = 1;
		} else if (pageNo > getTotalPage()) {
			this.pageNo = getTotalPage();
		}else {
			this.pageNo = pageNo;
		}
		if ((this.pageNo - 1) * this.pageSize >= totalCount) {
			this.pageNo = totalCount / pageSize;
		}
		if(totalCount>pageSize){//计算总页数
			this.pageNos=(totalCount+pageSize-1)/pageSize;
		}
	}

	@SuppressWarnings("unchecked")
	public Pagination(int pageNo, int pageSize, int totalCount, List list) {
		this(pageNo, pageSize, totalCount);
		this.list = list;
	}

	public int getFirstResult() {
		return (pageNo - 1) * pageSize;
	}
	
	/**
	 * 调整分页参数，使合理化
	 */
	public void adjustPage() {
		if (totalCount <= 0) {
			totalCount = 0;
		}
		if (pageSize <= 0) {
			pageSize = DEF_COUNT;
		}
		if (pageNo <= 0) {
			pageNo = 1;
		}
		if ((pageNo - 1) * pageSize >= totalCount) {
			pageNo = totalCount / pageSize;
		}
	}
	
	/**
	 * 总页数
	 */
	public int getTotalPage() {
		int totalPage = totalCount / pageSize;
		if (totalCount % pageSize != 0 || totalPage == 0) {
			totalPage++;
		}
		return totalPage;
	}

	public int getNextPage() {
		if (isLastPage()) {
			return pageNo;
		} else {
			return pageNo + 1;
		}
	}

	public int getPrePage() {
		if (isFirstPage()) {
			return pageNo;
		} else {
			return pageNo - 1;
		}
	}
	
	public String getPageOrder(){
		
		StringBuffer sb = new StringBuffer();
		//实际工具条数
		int toolBarCount = DEF_TOOLCOUNT >= getTotalPage() ? getTotalPage() : DEF_TOOLCOUNT;
		int leftbar = DEF_TOOLCOUNT%2 == 0 ? DEF_TOOLCOUNT/2 - 1 : DEF_TOOLCOUNT/2;
		int rightbar = DEF_TOOLCOUNT - leftbar;
		leftbar += (getTotalPage() - getPageNo() >= rightbar ? 0 : (rightbar -(getTotalPage() - getPageNo())-1));
		
		int j = getPageNo() - leftbar <= 1 ? 1 : getPageNo() - leftbar;
		
		
		for (; j < getPageNo(); j++) {		
			sb.append("<li><a " + (getPageNo() == j ? "class=\"actived\" >" + j + "</a>" : ( toolBarCount == DEF_TOOLCOUNT ? "href=\"javascript:_gotoPage(1);\" >1</a>" : "href=\"javascript:_gotoPage(" + j + ");\" >" + j + "</a>"))  + "</li>");
			toolBarCount --;
		}
		
		int count = getPageNo() + toolBarCount;
		
		for (int k = getPageNo(); k < count; k++) {
			sb.append("<li><a " + (getPageNo() == k ? "class=\"actived\" >" + k + "</a>" : ( (k+1) == count ? "href=\"javascript:_gotoPage("+getTotalPage()+");\" >"+getTotalPage()+"</a>" : "href=\"javascript:_gotoPage(" + k + ");\" >" + k + "</a>"))  + "</li>");
		}

		return sb.toString();
	}
	
	
	
	/**
	 * 工具条总数
	 * @return
	 */
	public int getToolSum(){
		int pageSum = getTotalPage(); //总数
		return pageSum%DEF_TOOLCOUNT == 0 ? pageSum/DEF_TOOLCOUNT : pageSum/DEF_TOOLCOUNT + 1;
	}
	
	public boolean isFirstPage() {
		return pageNo <= 1;
	}

	public boolean isLastPage() {
		return pageNo >= getTotalPage();
	}
	
	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	@SuppressWarnings("unchecked")
	public List getList() {
		return list;
	}

	@SuppressWarnings("unchecked")
	public void setList(List list) {
		this.list = list;
	}

	public int getToolNo() {
		return toolNo;
	}

	public void setToolNo(int toolNo) {
		this.toolNo = toolNo;
	}

	public int getPageNos() {
		return pageNos;
	}

	public void setPageNos(int pageNos) {
		this.pageNos = pageNos;
	}
}
